package com.module.circle.ui.selectDisplayMedal

import android.view.View
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.common.app.base.AppActivity
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.KeyEvents
import com.common.app.data.bean.circle.CirclesMedalBadgesBean
import com.common.app.data.bean.circle.CirclesMedalModel
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.getShowMsg
import com.common.base.app.extras.otherwise
import com.common.base.app.extras.showToast
import com.common.base.app.extras.yes
import com.common.base.utils.JsonUtils
import com.common.base.utils.StatusBarUtils
import com.common.base.widget.animator.CustomAnimation1
import com.jeremyliao.liveeventbus.LiveEventBus
import com.module.circle.R
import com.module.circle.databinding.CircleActivitySelectDisplayMedalBinding
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/08/14
 * <p>
 * 描述：选择展示勋章页面
 * <p>
 * 修订历史：
 */
class SelectDisplayMedalActivity :
    AppActivity<CircleActivitySelectDisplayMedalBinding, SelectDisplayMedalViewModel>(),
    View.OnClickListener {

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var mId: Int = 0

    @Autowired(name = KeyBundle.EXPAND_DATA)
    @JvmField
    var edMedalId: Int = 0

    var outMedalList: MutableList<CirclesMedalBadgesBean>? = null

    override fun getViewModel() = SelectDisplayMedalViewModel::class.java

    override fun getViewContentBinding() =
        CircleActivitySelectDisplayMedalBinding.inflate(layoutInflater)

    override fun getPageTitle(): String = ""
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    private val classicsFooter by lazy { ClassicsFooter(this) }

    private val mAdapter by lazy { SelectDisplayMedalAdapter() }

    private val headTitleUtils by lazy { HeadTitleUtils(this) }

    override fun initViews() {
        super.initViews()

        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        headTitleUtils.setTitle(getString(R.string.circle_ac_select_display_medal_title))
        headTitleUtils.setLineStyle(10)


        //上下拉刷新
        getRefreshFooter()?.let { mViewContentBinding.pdView.setRefreshFooter(it) }
        mViewContentBinding.pdView.setRefreshHeader(
            ClassicsHeader(this).setAccentColor(
                ContextCompat.getColor(this, R.color.text_content_week)
            )
        )
        classicsFooter.setAccentColor(
            ContextCompat.getColor(
                this,
                R.color.color_999999
            )
        )
        mViewContentBinding.pdView.setRefreshFooter(classicsFooter)

        mViewContentBinding.hrv.adapter = mAdapter
    }

    override fun initEvents() {
        super.initEvents()

        mViewContentBinding.pdView.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                lazyInitData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mViewContentBinding.pdView.setEnableLoadMoreWhenContentNotFull(false)
                mViewContentBinding.pdView.finishLoadMoreWithNoMoreData()
            }
        })
        mAdapter.setOnItemClickListener { _, view, position ->
            if (mAdapter.data[position].isShowOut) {
                return@setOnItemClickListener
            }
            requestChangeOutMedal(if (mAdapter.data[position].id == -999) edMedalId else (mAdapter.data[position].getCurBadge()?.id ?:0))
        }
    }

    override fun lazyInitData() {
        super.lazyInitData()
        getOutMedalList()
    }

    override fun onClick(v: View?) {
        when (v) {
//            mViewContentBinding.tvTitleRight -> LaunchHelper.launchWebActivity(ApiConstant.SECKILL_RULE)
        }
    }

    private fun finishPdView() {
        mViewContentBinding.pdView.finishRefresh()
        mViewContentBinding.pdView.finishLoadMore()
    }


    private fun getOutMedalList() {
        if (mId <= 0) {
            showErrorMsg()
            return
        }
        showDialogLoading()
        mViewModel.getOutMedalList(mId) {
            showToast(it.getShowMsg())
            finishPdView()
        }.observe(this) { rsoIt ->
            finishPdView()
            dismissLoadingDialog()
            rsoIt.isSuccess().yes {
                rsoIt.Data?.let { dataIt ->
                    outMedalList = dataIt as MutableList<CirclesMedalBadgesBean>
                    getMedalList()
                }
            }.otherwise {
                showToast(rsoIt.Message)
            }
        }
    }

    private fun getMedalList() {
        if (mId <= 0) {
            showErrorMsg()
            return
        }
        showDialogLoading()
        mViewModel.getMedalList(mId) {
            showToast(it.getShowMsg())
            finishPdView()
        }.observe(this) { rsoIt ->
            finishPdView()
            dismissLoadingDialog()
            rsoIt.isSuccess().yes {
                rsoIt.Data?.let { dataIt ->
                    val listTemp =
                        dataIt.filter { it.current_level >= 1 } as MutableList<CirclesMedalModel>
                    listTemp.add(
                        CirclesMedalModel(
                            id = -999,
                            name = getString(R.string.common_cancel_display_hint)
                        )
                    )
                    outMedalList?.forEach { outMedalListIt ->
                        listTemp.forEach { listTempIt ->
                            listTempIt.badges?.forEach { badgesIt ->
                                (outMedalListIt.id == badgesIt.id).yes {
                                    listTempIt.isShowOut = true
                                    return@forEach
                                }
                            }
                        }
                    }
                    val aasa = JsonUtils.toJson(listTemp)
                    mAdapter.setList(listTemp)
                }
            }.otherwise {
                showToast(rsoIt.Message)
            }
        }
    }


    private fun requestChangeOutMedal(mId: Int) {
        if (mId <= 0) {
            showToast(R.string.circle_not_select_display_medal_hint)
            return
        }
        showDialogLoading()
        mViewModel.requestChangeOutMedal(mId) {
            showToast(it.getShowMsg())
        }.observe(this) { rsoIt ->
            dismissLoadingDialog()
            rsoIt.isSuccess().yes {
//                getOutMedalList()
                LiveEventBus.get(KeyEvents.KEY_REFRESH_OUT_MEDAL_LIST).post(this)
                finish()
            }.otherwise {
                showToast(rsoIt.Message)
            }
        }
    }
}